# -*- coding: utf-8 -*-
"""
Created on Wed Mar 12 19:42:22 2014

@author: Maxim
"""
from airfoil import Airfoil2D
from airfoil_dialog import Ui_Form
from PyQt4 import QtGui
from numpy import array, zeros


class AirfoilWidget():
    """
    Widget that shows airfoil selection menu
    """
    def __init__(self):
        self.widget = QtGui.QWidget()
        self.af = Airfoil2D()
        self.ui = Ui_Form()
        self.ui.setupUi(self.widget)
        self.ui.pushButton.clicked.connect(self.open_text)
        self.ui.pushButton_3.clicked.connect(self.confirm)
        self.ui.pushButton_2.clicked.connect(self.preview)
        self.trailingEdgeThickness = None

    def confirm(self):
        self.create_airfoil()
        self.widget.hide()

    def preview(self):
        self.create_airfoil()
        self.af.display()

    def create_airfoil(self):
        try:
            if self.ui.radioButton.isChecked():
                self.read_text_file()
            elif self.ui.radioButton_2.isChecked():
                self.ui.comboBox_2.setCurrentIndex(0)
                self.naca4()
            elif self.ui.radioButton_3.isChecked():
                self.ui.comboBox_2.setCurrentIndex(0)
                self.cst()
            elif self.ui.radioButton_4.isChecked():
                self.ui.comboBox_2.setCurrentIndex(1)
                self.diamond()
            elif self.ui.radioButton_5.isChecked():
                self.ui.comboBox_2.setCurrentIndex(1)
                self.wedge()
        except:
            self.af.name = 'airfoil error'
        if self.ui.checkBox.isChecked():
            if self.ui.comboBox.currentText()=='mm':
                self.trailingEdgeThickness = [0,float(self.ui.lineEdit_7.text())] # option 0/1 is used for mm and chord ratio
            elif self.ui.comboBox.currentText()=='% of chord':
                self.trailingEdgeThickness = [1,float(self.ui.lineEdit_7.text())/100.]
        if self.ui.comboBox_2.currentText()=='spline':
            self.isSpline=True
        elif self.ui.comboBox_2.currentText()=='polyline':
            self.isSpline=False

    def read_text_file(self):
        path = self.ui.lineEdit.text()
        self.af.read_txt(path)

    def open_text(self):
        self.ui.lineEdit.setText(QtGui.QFileDialog.getOpenFileName())

    def naca4(self):
        thickness = float(self.ui.lineEdit_2.text())
        camber = float(self.ui.lineEdit_3.text())
        camberLoc = float(self.ui.lineEdit_4.text())
        self.af.naca4(thickness,camber,camberLoc)
    
    def cst(self):
        print 'CST options is not available yet'
    
    def diamond(self):
        tc = float(self.ui.lineEdit_8.text())/100. # thickness
        tcLoc = float(self.ui.lineEdit_9.text())/100. # thickness location
        up = array([[0.,0.],[tcLoc,tc/2],[1.,0.]])
        low = array([[0.,0.],[tcLoc,-tc/2],[1.,0.]])
        coord = array([[1.,0.],[tcLoc,tc/2],[0.,0.],[tcLoc,-tc/2],[1.,0.]])
        self.af.upPts = up
        self.af.loPts = low
        self.af.coord = coord
    def wedge(self):
        tc = float(self.ui.lineEdit_10.text())/100.
        ptLoc = float(self.ui.lineEdit_11.text())/100.
        ptLocTE = float(self.ui.lineEdit_12.text())/100.
        up = zeros([4,2])
        low = zeros([4,2])
        up[1] = [ptLoc,tc/2]
        up[2] = [1.-ptLocTE,tc/2]
        up[3] = [1.0,0.0]
        low[1] = [ptLoc,-tc/2]
        low[2] = [1.-ptLocTE,-tc/2]
        low[3] = [1.0,0.0]
        self.af.upPts = up
        self.af.loPts = low
        self.af._join_coord()